﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Model;

public partial class UserInfo_RolePage : BasePage
{
    RoleBO rbo = new RoleBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                TreeView1.Attributes.Add("onclick", "postBackByObject()");
                DataSet ds = access.GetMenuData2(Request.QueryString["id"].ToString());
                DataRow[] rows = ds.Tables[0].Select("ParentID=0");
                foreach (DataRow dr in rows)
                {
                    TreeNode tn = new TreeNode(dr["MenuName"].ToString(), dr["ID"].ToString());
                    if (dr["rid"].ToString() != "")   
                    tn.Checked = true;
                    TreeView1.Nodes.Add(tn);
                    AddChild(tn, ds, dr["ID"].ToString());
                }
            }
        }
    }

    public void AddChild(TreeNode tn, DataSet ds, string pid)
    {
        DataRow[] rows = ds.Tables[0].Select("ParentID="+pid);
        if (rows.Length > 0)
        {
            foreach (DataRow dr in rows)
            {
                TreeNode t = new TreeNode(dr["MenuName"].ToString(), dr["ID"].ToString());
                if (dr["rid"].ToString() != "")
                    t.Checked = true;
                tn.ChildNodes.Add(t);
                AddChild(t, ds, dr["ID"].ToString());
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        
        if (Request.QueryString["id"] != null)
        {
            rbo.Delete(Request.QueryString["id"].ToString());
            foreach (TreeNode tn in TreeView1.Nodes)
            {
                if (tn.Checked)
                {
                    Role r = new Role() { Demo = "", Id = 0, ParentID = int.Parse(tn.Value), RoleName = "", Seqence = int.Parse(Request.QueryString["id"].ToString()) };
                    rbo.Add(r);
                }
                ErgodicTreeView(tn);
                
            }
        }
        AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
    }

    void ErgodicTreeView(TreeNode tn)
    {
        if (tn == null) return;
        foreach (TreeNode n in tn.ChildNodes)
        {
            if (n.Checked)
            {
                Role r = new Role() { Demo = "", Id = 0, ParentID = int.Parse(n.Value), RoleName = "", Seqence = int.Parse(Request.QueryString["id"].ToString()) };
                rbo.Add(r);
            }
            ErgodicTreeView(n);
        }
    }

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
    
    }

    public void AddChild(TreeNode tn,bool check)
    {
            foreach (TreeNode t in tn.ChildNodes)
            {
                t.Checked = check;
                AddChild(t,check);
            }
       
    }
    protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        //TreeNode t = (TreeNode)sender;
        AddChild(e.Node, e.Node.Checked);
    }
}